//index.js
//获取应用实例
const Zan = require('../../bower_components/zanui-weapp/dist/index.js');
const app = getApp()
const ini = require('../../common/init/init.js');
const conf = require('../../common/conf/conf.js');
var thisApp = getApp();
var userInfo = wx.getStorageSync('userInfo')
var wxUserInfo = wx.getStorageSync('wxUserInfo')
var userInfo = wx.getStorageSync('userInfo')
Page(Object.assign({}, Zan.Field, Zan.Toast, {
  data: {
    userInfo: {},
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    memberIni: ini.memberPage,
  },
  onLoad: function () {
    // this.showToast();
    console.log('**************',userInfo)
    this.setData({
      'memberIni[0].value': userInfo.username ? userInfo.username:"",
      'memberIni[1].value': userInfo.job ? userInfo.job : "",
      'memberIni[2].value': userInfo.company ? userInfo.company : "",
      'memberIni[3].value': userInfo.mobile ? userInfo.mobile : "",
      'memberIni[4].value': userInfo.wechat ? userInfo.wechat : "",
      'memberIni[5].value': userInfo.addr ? userInfo.addr : "",
    })
    if (wx.getStorageSync('userInfo')) {
      this.setData({
        userInfo: wxUserInfo,
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
    handleZanFieldChange(e) {
    const { componentId, detail } = e;

    console.log('[zan:field:change]', componentId, detail);
  },
    handleZanFieldFocus(e) {
      const { componentId, detail } = e;

      console.log('[zan:field:focus]', componentId, detail);
    },
    handleZanFieldBlur(e) {
      const { componentId, detail } = e;

      console.log('[zan:field:blur]', componentId, detail);
    },
    clearInput() {
      this.setData({
        value: ''
      });
    },
    formSubmit(event) {
      var _this = this
      // var uid = thisApp.globalData.userInfo.uid
      console.log('###########', event.detail.value.username)
      var uid = '';
      uid = userInfo.id
      console.log('[zan:field:submit]', event.detail.value);
      wx.request({
        url: conf.infoPage.userInfoUrl + uid,
        data:{
          // userId:thisApp.globalData.userInfo,
          values: event.detail.value,
          username: event.detail.value.username,
          job: event.detail.value.job,
          company:event.detail.value.company,
          mobile:event.detail.value.phone,
          wechat:event.detail.value.wechat,
          avatar: wxUserInfo.avatarUrl
        },
        method:"PUT",
        dataType:'json',
        success:function(res){
          console.log(res)
          _this.showToast('修改资料成功');
        },
        fail:function(res){
          _this.showToast('修改资料失败，请重新尝试');
        }
      })
    },
    formReset(event) {
      console.log('[zan:field:reset]', event);
    },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  showToast(content) {
    this.showZanToast(content);
  },
  uploadHeadImg(){
    var _this = this
    wx.chooseImage({
      count:1,
      success: function(res) {
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: conf.infoPage.uploadHeadImgUrl,
          filePath: tempFilePaths,
          name: 'headImg',
          success:function(res){
            _this.setData({
              'userInfo.avatarUrl' : tempFilePaths              
            })
          }
        })
      },
    })
  }
}))
